package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.common.ui.BaseActivity;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Order;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.params.LoginParams;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.AndroidUtils;
import com.mondo.financialwallet.presentation.utils.LocalConfigStorage;
import com.mondo.financialwallet.presentation.utils.Toasts;

import java.util.List;

import butterknife.Bind;

public class LoginActivity extends BaseActivity {

    @Order(1)
    @Bind(R.id.mobile_number_edit)
    @NotEmpty(messageResId = R.string.message_please_input_account)
    EditText mMobileNumberEdit;

    @Order(2)
    @Bind(R.id.password_edit)
    @NotEmpty(messageResId = R.string.message_please_enter_your_password)
    EditText mPasswordEdit;

    @Bind(R.id.login_view) View mLoginView;
    @Bind(R.id.register_view) View mRegisterView;
    @Bind(R.id.forget_password_view) View mForgetPasswordView;
    @Bind(R.id.password_visibility_control) CheckBox mPasswordVisibilityControl;


    private Validator.ValidationListener mLoginValidationListener = new Validator.ValidationListener() {

        @Override
        public void onValidationSucceeded() {
            requestLogin(new LoginParams(mMobileNumberEdit.getText(), mPasswordEdit.getText()));
        }

        @Override
        public void onValidationFailed(List<ValidationError> errors) {
            if (!errors.isEmpty()) {
                Toasts.warning(errors.get(0).getCollatedErrorMessage(
                        LoginActivity.this));
            }
        }
    };


    @Override
    protected int getBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setWhiteDarkColorStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    protected void initActions() {
        // 登录验证
        Validator validator = new Validator(this);
        validator.setValidationListener(mLoginValidationListener);

        // 登录
        mLoginView.setOnClickListener(v -> validator.validate());

        // 跳转注册
        mRegisterView.setOnClickListener(v -> RegisterActivity.launch(this));

        // 显示获取隐藏密码
        AndroidUtils.contentVisibilityToggle(mPasswordVisibilityControl, mPasswordEdit);

        // 忘记密码
        mForgetPasswordView.setOnClickListener(v ->
                startActivity(new Intent(this, ForgetPasswordActivity.class))
        );
    }

    private void requestLogin(LoginParams params) {
        DataRepository.get().login(params, result -> {
            if (result.isSuccess()) {
                LocalConfigStorage.get().login(
                        result.getToken(),
                        params.getPassword(),
                        result.getUid(),
                        result.getMobile(),
                        result.getMobile_prefix()
                );

                LocalConfigStorage.get().bindGoogleValidator(result.isBindGoogle());
                LocalConfigStorage.get().bindEmail(result.isBindEmail());
                startActivityAndFinish(new Intent(this, MainActivity.class));
            } else {
                Toasts.failure(result.getMsg());
            }
        });
    }
}
